﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Collections;


/// <summary>
/// Represents a Multimedia Object. This is used when displaying databound lists of Multimedia.
/// </summary>
public class EmbeddedObject
{
	public EmbeddedObject(string ID, string URL, string Title, string Multimedia, string Image, string Content)
	{
        this.ID = ID;
        this.URL = URL;
        this.Title = Title;
        this.Multimedia = Multimedia;
        this.Image = Image;
        this.Content = Content;
	}

    public string ID
    {
        get;
        set;
    }

    public string URL
    {
        get;
        set;
    }

    public string Title
    {
        get;
        set;
    }

    public string Image
    {
        get;
        set;
    }

    public string Multimedia
    {
        get;
        set;
    }

    public string Content
    {
        get;
        set;
    }

    /// <summary>
    /// This method checks to make sure the YouTube ID is valid.
    /// </summary>
    /// <param name="ID">the unique YouTube ID</param>
    /// <returns>Whethere the ID is valid.</returns>
    public static bool isIDValid(string ID)
    {
        try
        {

            // Load XML API Document to grab information about the video
            XmlDocument gdata = new XmlDocument();
            gdata.Load("http://gdata.youtube.com/feeds/api/videos/" + ID);

            // If the XML Document can load then it is a valid ID
            return true;
        }
        catch
        {
            // If the XML Document cannot load then it is not a valid ID.
            return false;
        }

    }

    private static EmbeddedObject CreateEmbeddedObject(string ID)
    {

        // Construct embedded object parameter fields
        string id = ID;
        string url = "http://www.youtube.com/watch?v=" + ID;
        string title = "";
        string video = "http://www.youtube.com/v/" + ID;
        string image = "http://www.youtube.com/vi/" + ID + "/2.jpg";
        string content = "";


        try
        {

            // Load XML API Document to grab information about the video
            XmlDocument gdata = new XmlDocument();
            gdata.Load("http://gdata.youtube.com/feeds/api/videos/" + ID);

            XmlNodeList titleList = gdata.GetElementsByTagName("title");

            // Get title field
            if (titleList != null)
            {
                foreach (XmlNode titleNode in titleList)
                {
                    // Make sure the attribute is not null
                    if (titleNode.Attributes["type"] != null)
                    {
                        // We are looking for a specific attribute value
                        if (titleNode.Attributes["type"].Value == "text")
                        {
                            title = titleNode.InnerText;
                            break;
                        }
                    }
                }
            }

            XmlNodeList contentList = gdata.GetElementsByTagName("content");

            // Get content field
            if (contentList != null)
            {
                foreach (XmlNode contentNode in contentList)
                {
                    // Make sure the attribute is not null
                    if (contentNode.Attributes["type"] != null)
                    {
                        // We are looking for a specific attribute value
                        if (contentNode.Attributes["type"].Value == "text")
                        {
                            content = contentNode.InnerText;

                            // Truncate big content
                            if (content.Length > 290)
                            {
                                content = content.Substring(0, 287) + "...";
                            }

                            break;
                        }
                    }
                }
            }
        }
        catch { }

        return new EmbeddedObject(id, url, title, video, image, content);
    }


    public static List<EmbeddedObject> CreateList(string uniqueIDFields)
    {
        List<EmbeddedObject> embeddedObjects = new List<EmbeddedObject>();

        string[] uniqueIDArray;

        // If the string is not empty then split it and capture the IDs
        if (uniqueIDFields != "")
        {
            uniqueIDArray = uniqueIDFields.Split(';');
        }
        // If the string is empty return a 0 length string array
        else
        {
            uniqueIDArray = new string[0];
        }

        // Create embedded objects out of the string array
        for (int i = 0; i < uniqueIDArray.Length; i++)
        {
            EmbeddedObject newEmbeddedObject = CreateEmbeddedObject(uniqueIDArray[i]);

            embeddedObjects.Add(newEmbeddedObject);
        }


        return embeddedObjects;
    }

    public static string AddEmbeddedObject(List<EmbeddedObject> multimediaList, string idToAdd)
    {
        List<String> idList = new List<String>();

        // Create a List of ID's based off of the EmbeddedObject List.
        for (int i = 0; i < multimediaList.Count; i++)
        {
            idList.Add(multimediaList[i].ID);
        }

        // Add our new ID to the list
        idList.Add(idToAdd);

        
        string sqlCommand = "";

        // Create a semicolon delimited list of all of the ID's
        for (int i = 0; i < idList.Count; i++)
        {
            sqlCommand += idList[i];

            if (i != idList.Count - 1)
            {
                sqlCommand += ";";
            }
        }

        return sqlCommand;
    }

    public static string DeleteEmbeddedObject(List<EmbeddedObject> multimediaList, string idToDelete)
    {
        List<String> idList = new List<String>();

        // Create a List of ID's based off of the EmbeddedObject List.
        for (int i = 0; i < multimediaList.Count; i++)
        {
            idList.Add(multimediaList[i].ID);
        }
        
        // Iterate through the list and find the ID we wish to delete and delete it
        for (int i = 0; i < idList.Count; i++)
        {
            if (idList[i] == idToDelete)
            {
                idList.RemoveAt(i);
                break;
            }
        }

        string sqlCommand = "";

        // Create a semicolon delimited list of all of the ID's
        for (int i = 0; i < idList.Count; i++)
        {
            sqlCommand += idList[i];

            if (i != idList.Count - 1)
            {
                sqlCommand += ";";
            }
        }

        return sqlCommand;
    }

}